package com.web.app.ctrl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.web.app.dao.DiscussCommentMapper;
import com.web.app.dao.DiscussMapper;
import com.web.app.pojo.Discuss;
import com.web.app.pojo.DiscussComment;
import com.web.app.pojo.MyDiscuss;
import com.web.app.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 讨论表 前端控制器
 * </p>
 *
 * @author 林
 * @since 2022-09-21
 */
@RestController
@CrossOrigin
public class DiscussController {
    @Autowired
    private DiscussMapper discussMapper;

    @Autowired
    private DiscussCommentMapper commentMapper;

    /**
     * 获取评论信息
     * @param id 关联的班级id
     * @return
     */
    @RequestMapping("/getDiscuss")
    public ResultUtil getDiscuss(int id){
        ResultUtil resultUtil = ResultUtil.ok();
        List<MyDiscuss> discusses = discussMapper.getDiscussByClassId(id);
        resultUtil.setData(discusses);
        return resultUtil;
    }

    @RequestMapping("/addDiscuss")
    public ResultUtil addDiscuss(Discuss discuss){
        ResultUtil resultUtil = ResultUtil.ok();
        discuss.setCreateTime(new Date());
        discussMapper.insert(discuss);
        return resultUtil;
    }

    @RequestMapping("/updateComment")
    public ResultUtil updateComment(DiscussComment comment){
        ResultUtil resultUtil = ResultUtil.ok();
        comment.setCreateTime(new Date());
        commentMapper.insert(comment);
        return resultUtil;
    }

    @RequestMapping("/deleteOutDiscuss")
    public ResultUtil deleteOutDiscuss(int id){
        ResultUtil resultUtil = ResultUtil.ok();
        discussMapper.deleteById(id);
        QueryWrapper<DiscussComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("relation_discuss_id",id);
        commentMapper.delete(queryWrapper);
        return resultUtil;
    }

    @RequestMapping("/deleteIn")
    public ResultUtil deleteIn(int id){
        ResultUtil resultUtil = ResultUtil.ok();
        commentMapper.deleteById(id);
        return resultUtil;
    }
}

